package es.ucm.disia.rcei.example.voice2;

import java.io.File;

import es.ucm.disia.rcei.RCEICommand;
import es.ucm.disia.rcei.RCEIMessageImpl;
import es.ucm.disia.rcei.RCEModel;
import es.ucm.disia.rcei.RCEModelImpl;

public class OrderDispatcher
{
	//private long timeSleep;
	private RCEModel rceModel;
	//private VoiceRecognizer voiceRecognizer;
	private VoiceRecognizer processRecognizer;
	
	public OrderDispatcher(String rceFilename)
	{
		//voiceRecognizer=new VoiceRecognizer("rceiVoice.config.xml");
		processRecognizer=new VoiceRecognizer("rceiVoice.config.xml"); //"hellongram.config.xml"
		startup();
		
		try
		{
			rceModel = new RCEModelImpl(rceFilename);
			// two seconds
			//timeSleep = 2000;
			rceModel.connect();
		}
		catch (Exception e)	{}
	}
	
	
	public RCEICommand generateCommandText(String resultText)
	{
		RCEICommand cmd = null;// new RCEICommand("agent"+resultText,"speak");
		System.out.println(resultText);
		String phrase = "none";
		String subject = "beggar";
		// Cases
		if (resultText.equalsIgnoreCase("one"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("one");
		} else if (resultText.equalsIgnoreCase("two"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("two");
		} else if (resultText.equalsIgnoreCase("three"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("three");
		} else if (resultText.equalsIgnoreCase("four"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("four");
		} else if (resultText.equalsIgnoreCase("four"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("agent4 presente Dios mio no siento las piernas");
		} else if (resultText.equalsIgnoreCase("five"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("five");
		} else if (resultText.equalsIgnoreCase("six"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("six");
		} else if (resultText.equalsIgnoreCase("seven"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("seven");
		} else if (resultText.equalsIgnoreCase("eight"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("eight");
		} else if (resultText.equalsIgnoreCase("nine"))
		{
			cmd = new RCEICommand(subject, "speak");
			cmd.setDirComp("nine");
		}
		else if (resultText.equalsIgnoreCase("Paul")||resultText.equalsIgnoreCase("Will")||
				 resultText.equalsIgnoreCase("Rita")||resultText.equalsIgnoreCase("Evandro")||
				 resultText.equalsIgnoreCase("Philip")||resultText.equalsIgnoreCase("Bhiksha"))
		{
			System.out.println(resultText);
			cmd = new RCEICommand("environment", "search");
			cmd.setDirComp(resultText);//"Paul");
		}

		return cmd;
	}
	
	public void startup() 
    {
		try 
        {
			System.out.println("startup recognizer...");
			//voiceRecognizer.startup();
			processRecognizer.startup();
        }
		catch (Throwable e) 
        {
        	System.out.println("Error: " + e.getMessage());
        }
    }
	
	public void shutdown() 
    {
		try 
        {
			System.out.println("shutdown recognizer...");
			processRecognizer.shutdown();
        }
		catch (Throwable e) 
        {
        	System.out.println("Error: " + e.getMessage());
        }
    }
	
	/**Perform any needed initializations and then enable the 'speak'
     * button, allowing recognition to proceed*/
    public void recognize() 
    {
        try 
        {
            System.out.println("Starting recognizer...");            
            //voiceRecognizer.run();
        }
        catch (Throwable e) 
        {
        	System.out.println("Error: " + e.getMessage());
        }
    }
    
    public void recognizeProcess() 
    {
        try 
        {
            System.out.println("Starting recognizer...");            
            processRecognizer.run();
            //return processRecognizer.getOrderRecognized();
        }
        catch (Throwable e) 
        {
        	System.out.println("Error: " + e.getMessage());
        	//return null;
        }
    }
    
    public String getOrderRecognized()
	{
    	return processRecognizer.getOrderRecognized();
	}
	
	public RCEICommand generateCommandAction(String subject, String process)
	{
		RCEICommand cmd = null;// new RCEICommand("agent"+resultText,"speak");
		System.out.println(subject);

		// Cases
		if (process.equalsIgnoreCase("close"))
		{
			cmd = new RCEICommand("environment", "open");
			cmd.setDirComp("door");
		}
		else if (process.equalsIgnoreCase("open"))
		{
			cmd = new RCEICommand("environment", "close");
			cmd.setDirComp("door");
		} 
		else if (process.equalsIgnoreCase("attack"))
		{
			cmd = new RCEICommand(subject, "attack");
			cmd.setDirComp("oldwoman");
		} 
		else if (process.equalsIgnoreCase("move"))
		{
			cmd = new RCEICommand(subject, "go");			
		} 
		else if (process.equalsIgnoreCase("search"))
		{
			cmd = new RCEICommand("environment", "search");
			cmd.setDirComp(subject);
			cmd.setPlaceComp("cime_reuel1");
		}
		//if the process is empty
		else if (subject.equalsIgnoreCase("Paul")||subject.equalsIgnoreCase("Will")||
				 subject.equalsIgnoreCase("Rita")||subject.equalsIgnoreCase("Evandro")||
				 subject.equalsIgnoreCase("Philip")||subject.equalsIgnoreCase("Bhiksha"))
		{
			System.out.println(subject);
			cmd = new RCEICommand("environment", "search");
			cmd.setDirComp(subject);//"Paul");
		}
		
		return cmd;
	}
	
	public void sendCmd (RCEICommand cmd)
	{
		if (cmd != null)
		{
			RCEIMessageImpl msg = new RCEIMessageImpl();
			msg.addInstruction(cmd);				
			rceModel.send(msg);
		}
	}

	public static void main(String args[]) throws InterruptedException
	{
		System.out.println("RCEI VOICE!...");
		OrderDispatcher hd = new OrderDispatcher("rce" + File.separator+ "nwn1.rcei");
		RCEICommand cmd=null;
		String speech, subject, process;
		int i=2;
		long tiempo1=System.currentTimeMillis();		
		//hd.startup();
		//hd.go();
		long tiempo2=System.currentTimeMillis();
		System.out.println("time " + ((tiempo2-tiempo1)/1000) + "segs");
		
		while (i>0)
		{
			tiempo1=System.currentTimeMillis();
			//speech = 
			hd.recognizeProcess();
			tiempo2=System.currentTimeMillis();
			System.out.println(" time " + ((tiempo2-tiempo1)/1000) + "segs");
			
			speech = hd.getOrderRecognized();
			System.out.println(speech);
			
			if (speech.contains(" "))
			{
				subject= speech.substring(0,speech.indexOf(" "));
				process= speech.substring(speech.indexOf(" "),speech.length());
				cmd = hd.generateCommandAction(subject, process);
			}
			else if (speech!="")
			{
				subject=speech;
				cmd = hd.generateCommandAction(subject, "");				
			}
			
			hd.sendCmd(cmd);
			
			System.out.println("Iteracion "+i);
			i--;
			Thread.sleep(2000);
		}
		// It should be: examples\pig\rce\nwn1.rcei
		//hd.run();beggar
		
		/*cmd = new RCEICommand("beggar", "search");
		cmd.setDirComp("rita");//"Paul");
		hd.sendCmd(cmd);*/
	}
}
